<template>
  <div id="app">
    <el-header>
      <el-menu
        :default-active="activeIndex2"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <el-menu-item index="1"
          ><router-link :to="`/home/index`">首页</router-link></el-menu-item
        >
        <el-menu-item index="2"
          ><router-link :to="`/order/index`"
            >我的订单</router-link
          ></el-menu-item
        >
        <el-menu-item index="3" style="float: right" v-show="!loginState">
          <a @click="login">登录</a></el-menu-item
        >
        <el-menu-item index="4" style="float: right" v-show="!loginState">
          <a @click="showRegister">注册</a></el-menu-item
        >
        <el-submenu index="5" style="float: right" v-show="loginState">
          <template slot="title">{{ userName }}</template>
          <el-menu-item index="2-1" @click="logout">退出</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-header>
    <el-dialog
      title="注册"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" size="small" @click="handleRegister"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <router-view />
  </div>
</template>

<script>
import Oidc from "oidc-client";
import * as user from "@/api/user";
import store from "./store";

let config = process.env.OIDC;
let mgr = new Oidc.UserManager(config);

export default {
  name: "App",
  data() {
    return {
      activeIndex: "1",
      activeIndex2: "1",
      loginState: false,
      dialogVisible: false,
      userName: undefined,
      form: {
        username: undefined,
        password: undefined,
      },
    };
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    handleSelect(key, keyPath) {
      //console.log(key, keyPath);
    },
    //调用Ids4服务进行登录
    login() {
      mgr.signinRedirect();
    },
    //退出登录
    logout() {
      mgr.signoutRedirect({ id_token_hint: store.getters.getIdToken });
      //清空LocalStorage
      localStorage.clear();
    },
    //显示注册
    showRegister() {
      this.dialogVisible = true;
    },
    //注册
    handleRegister() {
      let data = { ...this.form };
      user.register(data).then((response) => {
        this.$message(response.result);
        this.handleClose();
      });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    //获取用户信息
    getUserInfo() {
      if (store.getters.getUserName) {
        this.loginState = true;
        this.userName = store.getters.getUserName;
      }
    },
  },
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}
</style>
